<html>
	<head>
        <title>Recommend target</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="main.css">
        <script type="text/javascript">
	        var cval = 0;
	        
	        function spamgen(){
	            var rnumber = Math.floor(Math.random()*995);
	            //So previously incorrect input string is not accidentally correct
	            while((rnumber + 3) == document.getElementById('spam').value){
	                rnumber = Math.floor(Math.random()*995);
	            }
	            document.getElementById('m1').innerHTML = "<b>" + rnumber + "</b>";
	            document.getElementById('m2').innerHTML = "<b>" + (rnumber + 1) + "</b>";
	            document.getElementById('m3').innerHTML = "<b>" + (rnumber + 2) + "</b>";
	            cval = rnumber + 3;
	            document.getElementById('m4').innerHTML = "<b>" + (rnumber + 4) + "</b>";
	            document.getElementById('m5').innerHTML = "<b>" + (rnumber + 5) + "</b>";
	        }
	        
	        function checkspam(){
	            if(document.getElementById('spam').value != cval){
	                document.getElementById('warning').innerHTML = "<font color='red'> - Incorrect Answer</font>";
	                return 0;
	            } else{
	                document.getElementById('warning').innerHTML = "";
	                return 1;
	            }
	        }
	        
	        function clear_form(){
	            document.getElementById('name').value = "";
	            document.getElementById('email').value = "";
	            document.getElementById('feed').value = "";
	            document.getElementById('spam').value = "";
	            document.getElementById('warning').innerHTML = "";
	            spamgen();
	        }
	        
	        function give_feed(){
	            if(checkspam() == 1){
	                document.getElementById("feedform").submit();
	            }else{
	                spamgen();
	            }
	            
	        }
    	</script>
    </head>
    
    <% String name=(String)session.getAttribute("username");

	if(name==null){
    	out.print("Please login first! redirecting to Login...");
    	response.setHeader("Refresh","1;URL=login.jsp");
	}
	else{
	%>
    
<body>
<%@ page import="java.lang.*,java.util.*,javax.mail.*,javax.mail.internet.*,javax.activation.*, ejb.GMailAuthenticator" %>

<%
if(request.getParameter("name") != null){
	try{
	        String host = "smtp.gmail.com";
	        String from = "gfsweekendplanner@gmail.com";
	        String pass = "weareteamfour";
	        Properties props = System.getProperties();
	        props.put("mail.smtp.starttls.enable", "true");
	        props.put("mail.smtp.host", host);
	        props.put("mail.smtp.user", from);
	        props.put("mail.smtp.password", pass);
	        props.put("mail.smtp.port", "587");
	        props.put("mail.smtp.auth", "true");
	        props.put("mail.debug", "false");
	        
	        GMailAuthenticator st = new GMailAuthenticator("gfsweekendplanner", "weareteamfour");
	
	        Session session1 = Session.getDefaultInstance(props, st);
	        MimeMessage message = new MimeMessage(session1);
	        Address fromAddress = new InternetAddress(from);
	        Address toAddress = new InternetAddress(request.getParameter("email"));
	        String mailBody = "Dear "+request.getParameter("name")+",<br /><br />Your friend has just sent this recommendation for you!<br /> He/She says:<br />"+request.getParameter("feed")+"<br /><br />Check it out now!  <br /> <a href='http://localhost:8080/WPro/TargetServlet?targetID="+ request.getParameter("targetID")  +"' target='_blank'>"+request.getParameter("targetname")+"</a>";
			
			//String targetname = request.getParameter("targetname");
			//mailBody = mailBody + "<a href='TargetServlet?targetID="+ request.getParameter("targetID")  +"' target='_blank'>"+request.getParameter("targetname")+"</a>";
			
	        message.setFrom(fromAddress);
	        message.setRecipient(Message.RecipientType.TO, toAddress);
	        message.setSubject("Your friend has just recommended a target for you!");
	        message.setText(mailBody, "UTF-8", "html");
	        Transport transport = session1.getTransport("smtp");
	        transport.connect(host, from, pass);
	        message.saveChanges();
	        Transport.send(message);
	        transport.close();
	        out.println("Thank you! Your recommendation has been sent successfully and is on its way to your friend!</br></br><a href='javascript:window.close()''>Close this Window</a></br></br> or Send another one:</br></br>");
	
	    }catch(Exception ex){
	        out.println("There was a problem while sending your feedback");
	        out.println("Please mannually send error information to gfsweekendplanner@gmail.com");
	        out.println("We are sorry for any inconvenience");
	        out.println("ERROR DETAIL : " + ex);
	    }
}
%>


		<div></div>
        <h3>Send Recommendation for <% out.println(request.getParameter("targetname")); %> to your Friend NOW!</h3>
        <form id="feedform" action="SendRecommend.jsp">
            Name: <input id="name" name="name" "type="text"></input> &nbsp;
            Email: <input id="email" name="email" type="text"></input><br><br>
            Recommendation detail<br>
            <textarea id="feed" name="feed" rows="10" style="width:500px;"></textarea>
            <br><br>
            <font color="green">Anti-spam >>></font> &nbsp;
            Fill in the missing number: &nbsp;
            <span id="m1"></span> - <span id="m2"></span> - <span id="m3"></span> - <input id="spam" type="text" style="width:30px"></input> - <span id="m4"></span> - <span id="m5"></span> &nbsp; &nbsp; <span id="warning"></span>
            <br><br>
            <input type="button" onclick="clear_form()" value="Reset"></input>
            <input type="button" onclick="give_feed()" value="Give Recommendation!"></input>
            <input type="hidden" name="targetname" value="Click Here For Link" />   
            <input type="hidden" name="targetID" value="<% out.println(request.getParameter("targetID")); %>" />      
            
        </form>
         
        <script>
            window.onload = function(){
                clear_form();
            }
        </script>

</body>

<% } %>

</html>
